<project basedir="." default="beanstalk">
	<property file="build.properties" />

	<macrodef name="init">
		<sequential>
			<property name="work.dir" location="work/" />
			<property name="build.dir" location="${work.dir}/build/" />
			<property name="build.bin.dir" location="${build.dir}/bin/" />
			<property name="build.lib.dir" location="${build.dir}/" />
			<property name="build.src.dir" location="${build.dir}/bin/" />
			<property name="build.web.dir" location="${build.dir}/web/" />

			<property name="target.dir" location="target/" />
			<property name="target.src.dir" location="${target.dir}/java/" />
			<property name="target.web.dir" location="${target.dir}/web/" />
			<property name="target.webinf.dir" location="${target.web.dir}/WEB-INF/" />
			<property name="target.lib.dir" location="${target.webinf.dir}/lib/" />
			<available property="target.lib.exists" file="${target.lib.dir}" />
			<available property="target.src.exists" file="${target.src.dir}" />

			<property name="beanstalk.dir.from" location="${target.dir}/beanstalk" />
			<property name="beanstalk.dir.to" location="${build.web.dir}/.ebextensions" />

			<property name="deprecated.dir" value="deprecated" />
			<property name="deprecated.lib.dir" value="${deprecated.dir}/lib/" />
			<property name="deprecated.src.dir" value="${deprecated.dir}/java/" />
			<available property="deprecated.lib.exists" file="${deprecated.lib.dir}" />
			<available property="deprecated.src.exists" file="${deprecated.src.dir}" />

			<property name="java.dir" location="C:/Program Files/Java/jdk1.8.0_25/" />
			<property name="compile.exe" location="${java.dir}/bin/javac.exe" />
			<property name="compile.encode" value="UTF8" />
			<property name="compile.version" value="1.8" />
			<property name="jar.exe" location="${java.dir}/bin/jar.exe" />
			<property name="war.exe" value="${jar.exe}" />

			<property name="target.jars" value="" />
			<property name="deprecated.jars" value="" />



			<property name="targetos" value="windows" />
			<path id="project.name.tmp1">
				<dirset dir="." excludes="**\\*" />
			</path>
			<property name="project.name.tmp2" location=".." />
			<pathconvert targetos="${targetos}" property="project.name" refid="project.name.tmp1">
				<map from="${project.name.tmp2}/" to="" />
				<map from="${project.name.tmp2}" to="" />
			</pathconvert>
			<property name="project.subname" value="" />

			<property name="jar.main.class" value="" />
			<property name="jar.manifest.name" value="jar.mani.txt" />
			<property name="jar.manifest.path" location="${build.dir}/${jar.manifest.name}" />
			<property name="jar.ext" value="zip" />
			<property name="jar.name" value="${project.name}${project.subname}.${jar.ext}" />
			<property name="jar.path" location="${target.dir}/${jar.name}" />
			<property name="build.user.name" value="${user.name}" />
			<property name="war.name" value="${project.name}${project.subname}.war" />
			<property name="war.path" value="${target.dir}/${war.name}" />
			<property name="webxml.path" location="${build.dir}/web.xml" />

			<property name="initialized" value="true" />
		</sequential>
	</macrodef>



	<macrodef name="clean">
		<sequential>
			<delete dir="${build.dir}" />
		</sequential>
	</macrodef>



	<macrodef name="copy.if.exists">
		<attribute name="from" />
		<attribute name="to" />
		<sequential>
			<available property="exists.@{from}" file="@{from}" />
			<antcall target="__copy.if">
				<param name="from" value="@{from}" />
				<param name="to" value="@{to}" />
			</antcall>
			<antcall target="__copy.unless">
				<param name="from" value="@{from}" />
				<param name="to" value="@{to}" />
			</antcall>
		</sequential>
	</macrodef>
	<target name="__copy.if" if="exists.${from}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>copy ${from} -&gt; ${to}</echo>
		<copy todir="${to}">
			<fileset dir="${from}" />
		</copy>
	</target>
	<target name="__copy.unless" unless="exists.${from}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>non copy [${from} is not exists.]</echo>
	</target>



	<macrodef name="copyjar.extra2build">
		<sequential>
			<path id="extra.target.jars">
				<filelist files="${target.jars}" />
				<filelist files="${deprecated.jars}" />
			</path>
			<copy todir="${build.lib.dir}" flatten="true">
				<path refid="extra.target.jars" />
			</copy>
		</sequential>
	</macrodef>





	<target name="compile">
		<init />
		<clean />
		<mkdir dir="${build.bin.dir}" />
		<mkdir dir="${build.src.dir}" />
		<mkdir dir="${build.lib.dir}" />

		<copy.if.exists from="${deprecated.lib.dir}" to="${build.lib.dir}" />
		<copy.if.exists from="${deprecated.src.dir}" to="${build.src.dir}" />
		<copy.if.exists from="${target.lib.dir}" to="${build.lib.dir}" />
		<copy.if.exists from="${target.src.dir}" to="${build.src.dir}" />
		<copyjar.extra2build />

		<path id="target.classpath">
			<fileset dir="${build.lib.dir}" includes="**/*.jar,**/*.zip" />
		</path>
		<pathconvert targetos="${targetos}" property="target.classpath.string" refid="target.classpath" pathsep=";">
			<map from="${build.lib.dir}/" to="" />
			<map from="${build.lib.dir}" to="" />
		</pathconvert>
		<echo>javac version="${compile.version}" encode="${compile.encode}" classpath="${target.classpath.string}"</echo>
		<javac srcdir="${build.src.dir}" destdir="${build.bin.dir}" includeantruntime="false" debug="true" fork="yes" encoding="${compile.encode}" executable="${compile.exe}" source="${compile.version}" target="${compile.version}">
			<classpath refid="target.classpath" />
			<include name="**/*.java" />
		</javac>
	</target>



	<target name="jar" depends="compile">
		<delete file="${jar.path}" />
		<path id="jar.classpath">
			<fileset dir="${build.lib.dir}" includes="**/*.jar,**/*.zip" />
		</path>
		<pathconvert targetos="${targetos}" property="jar.classpath.string" refid="jar.classpath" pathsep=" ">
			<map from="${build.lib.dir}/" to="" />
			<map from="${build.lib.dir}" to="" />
		</pathconvert>
		<jar jarfile="${jar.path}" compress="true" index="false" basedir="${build.bin.dir}">
			<include name="**/*.class" />
			<include name="**/*.java" />
			<manifest>
				<attribute name="Built-By" value="${build.user.name}" />
				<attribute name="Class-Path" value="${jar.classpath.string}" />
				<attribute name="Created-By" value="${compile.version}" />
				<attribute name="Main-Class" value="${jar.main.class}" />
			</manifest>
		</jar>
		<clean />
	</target>



	<!--
	<target name="jarcmd" depends="compile">
		<delete file="${jar.path}" />

		<path id="jar.classpath">
			<fileset dir="${build.lib.dir}" includes="**/*.jar,**/*.zip" />
		</path>
		<pathconvert targetos="${targetos}" property="jar.classpath.string" refid="jar.classpath" pathsep=" ">
			<map from="${build.lib.dir}/" to="" />
			<map from="${build.lib.dir}" to="" />
		</pathconvert>
		<concat append="false" destfile="${jar.manifest.path}">Built-By: ${build.user.name}
Main-Class: ${jar.main.class}
Class-Path: ${jar.classpath.string}
</concat>

		<property name="tmpjarcmd">"${jar.exe}" cfm ${jar.path} ${jar.manifest.path} .</property>
		<echo>create jar</echo>
		<echo>  ${tmpjarcmd}</echo>
		<exec executable="${tmpjarcmd}" dir="${build.bin.dir}" />
		<clean />
	</target>
	-->



	<target name="war" depends="jar">
		<create_war />
	</target>
	<target name="war_production" depends="jar">
		<create_war prefix="production." />
	</target>
	<target name="war_staging" depends="jar">
		<create_war prefix="staging." />
	</target>
	<target name="war_development" depends="jar">
		<create_war prefix="development." />
	</target>
	<target name="beanstalk" depends="jar">
		<property name="copy4beanstalk" value="yes" />
		<create_war prefix="" />
	</target>
	<target name="beanstalk_production" depends="jar">
		<property name="copy4beanstalk" value="yes" />
		<create_war prefix="production." />
	</target>
	<target name="beanstalk_staging" depends="jar">
		<property name="copy4beanstalk" value="yes" />
		<create_war prefix="staging." />
	</target>
	<target name="beanstalk_development" depends="jar">
		<property name="copy4beanstalk" value="yes" />
		<create_war prefix="development." />
	</target>
	<macrodef name="create_war">
		<attribute name="prefix" default="" />
		<sequential>
			<delete file="${war.path}" />

			<copy todir="${build.web.dir}">
				<fileset dir="${target.web.dir}" />
			</copy>
			<copy todir="${build.web.dir}/WEB-INF/lib" file="${jar.path}" />



			<antcall target="__copy_beanstalk" />



			<echo>file contents replace.</echo>
			<echo>  replace rule</echo>
			<echo>    \u000a &lt;- line feed</echo>
			<echo>    \u000d &lt;- carriage return</echo>
			<echo>    \u0020 &lt;- space</echo>
			<echo>    \u002e &lt;- period[.]</echo>
			<echo>    \u003a &lt;- colon[:]</echo>
			<echo>    \u003d &lt;- equal[=]</echo>
			<echo>    \u003f &lt;- question[?]</echo>
			<replace_macro prefix="@{prefix}" suffix="" ext=".config" />
			<replace_macro prefix="@{prefix}" suffix="" ext=".html" />
			<replace_macro prefix="@{prefix}" suffix="" ext=".jsp" />
			<replace_macro prefix="@{prefix}" suffix="" ext=".properties" />
			<replace_macro prefix="@{prefix}" suffix="" ext=".xml" />
			<replace_macro prefix="@{prefix}" suffix=".config" />
			<replace_macro prefix="@{prefix}" suffix=".html" />
			<replace_macro prefix="@{prefix}" suffix=".jsp" />
			<replace_macro prefix="@{prefix}" suffix=".properties" />
			<replace_macro prefix="@{prefix}" suffix=".xml" />



			<property name="tmpwarcmd">"${war.exe}" cfM ${war.path} .</property>
			<echo>create war</echo>
			<echo>  ${tmpwarcmd}</echo>
			<exec executable="${tmpwarcmd}" dir="${build.web.dir}" />

			<clean />
		</sequential>
	</macrodef>
	<macrodef name="replace_macro">
		<attribute name="prefix" default="" />
		<attribute name="suffix" default="" />
		<attribute name="ext" default="@{suffix}" />
		<sequential>
			<available property="exists.@{prefix}replace@{suffix}" file="@{prefix}replace@{suffix}.properties" />
			<antcall target="__replace_target">
				<param name="prefix" value="@{prefix}" />
				<param name="suffix" value="@{suffix}" />
				<param name="ext" value="@{ext}" />
			</antcall>
		</sequential>
	</macrodef>
	<target name="__replace_target" if="exists.${prefix}replace${suffix}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<echo>Replace by ${prefix}replace${suffix}.properties [*${ext}]</echo>
		<replace dir="${build.web.dir}" replacefilterfile="${prefix}replace${suffix}.properties" summary="on">
			<include name="**/*${ext}" />
		</replace>
	</target>
	<target name="__copy_beanstalk" if="${copy4beanstalk}">
		<fail unless="${initialized}" message="[Please initialize.]" />
		<copy.if.exists from="${beanstalk.dir.from}" to="${beanstalk.dir.to}" />
	</target>
</project>